﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using prototype123.models;
using prototype123.controllers;
using System.Collections;


namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //educator table in the database.
    class EducatorDAO
    {
        private SqlConnection ed_connection; // = prototype123.Program.connect.GetConnection();

        public EducatorDAO(SqlConnection connection)
        {
            ed_connection = connection;

        }

        /*precondition: the system has the educator ID and needs all related educator info
        description: given the ID number of an educator, query the db and get all related fields and create
              an educator object. return the educator object, or return null if the connection fails
        postcondition: system receives an educator object with all attributes populated*/
        public Educator Load(int id)
        {
            Educator ed = new Educator();
            SqlCommand getEd;
            ArrayList list = new ArrayList();

            try
            {
                getEd = new SqlCommand("SELECT * FROM EDUCATOR WHERE EDU_ID = " + id, ed_connection);
                SqlDataReader reader = getEd.ExecuteReader();
                while (reader.Read())
                {
                    ed.EducatorID = Convert.ToInt32(reader["EDU_ID"]);
                    ed.FirstName = (string)reader["FNAME"];
                    ed.LastName = (string)reader["LNAME"];
                    ed.FormalSalutation = (string)reader["FORMAL_SAL"];
                    ed.InformalSalutation = (string)reader["INFORMAL_SAL"];
                    ed.HomePhone = (string)reader["HOME_PHONE"];
                    ed.EmailAddress = (string)reader["EMAIL_ADDR"];
                    ed.SpouseFirstName = (string)reader["SPOUSE_FNAME"];
                    ed.SpouseLastName = (string)reader["SPOUSE_LNAME"];
                }
                reader.Close();
            }
            catch { ed = null; }
            return ed;
        }

    }
}
